﻿using System;
using System.Web.Mvc;
using IloveRSS.Models;
using IloveRSS.Core;


namespace IloveRSS.Controllers
{
    public class HomeController : Controller
    {
        private DataManager dataManager;
     
        public HomeController(DataManager dt)
        {
            this.dataManager = dt;
        }

        [OutputCache(Duration=1)]
        public ActionResult Index()
        {
            if (HttpContext.User.Identity.IsAuthenticated)
            {
                ViewBag.FavouriteChannel = dataManager.GetFavouriteChannel(HttpContext.User.Identity.Name);
                ViewBag.ChannelList = dataManager.GetChannelsByUser(HttpContext.User.Identity.Name);    
            }
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult AddChannel()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddChannel(string channelName, bool visible, string siteName1,
            string siteName2, string siteName3, string tags, string description)
        {
            if (channelName.Length < 1)
            {
                ModelState.AddModelError("channelName", "Поле \"название\" не может быть пустым");
            }
            if (siteName1.Length < 1)
            {
                ModelState.AddModelError("siteName1", "Поле \"Сайт 1\" не может быть пустым");
            }
            if (TextConverter.TagsAreNotValid(tags))
            {
                ModelState.AddModelError("tags", "Поле \"Теги\" не заполнено или содержит недопустимые символы");
            }
            if (description.Length < 1)
            {
                ModelState.AddModelError("description", "Поле \"Описание\" не может быть пустым");
            }

            if (ModelState.IsValid)
            {
                dataManager.AddChannel(HttpContext.User.Identity.Name, channelName, description, visible,
                    TextConverter.ConvertToTagList(tags),
                    TextConverter.ConvertToSiteList(siteName1, siteName2, siteName3));
                return RedirectToAction("Index", "Home"); 
            }
            ViewBag.Message = "Данные успешно добавлены";
            return View();
        }

        public ActionResult GetSites(string q)
        {
            return Content(dataManager.GetAllSitesByPrefix(q));
        }

        public ActionResult DeleteFromFavourite(int id)
        {
            dataManager.ResetFavouriteChannel(HttpContext.User.Identity.Name);
            return RedirectToAction("Index");
        }

        public ActionResult AddToFavourite(int id)
        {
            dataManager.ResetFavouriteChannel(HttpContext.User.Identity.Name);
            dataManager.SetFavouriteChannel(HttpContext.User.Identity.Name, id);
            return RedirectToAction("Index");
        }

        public ActionResult SignOut(int id)
        {
            dataManager.SignOut(HttpContext.User.Identity.Name, id);
            return RedirectToAction("Index"); 
        }

        public ActionResult SignIn(int id)
        {
            dataManager.SignIn(HttpContext.User.Identity.Name, id);
            return RedirectToAction("Index");
        }
        [OutputCache(Duration = 1)]
        public ActionResult ViewChannel(int id)
        {
            try
            {
                if(HttpContext.User.Identity.IsAuthenticated)
                {
                    if(dataManager.IsVisibleByUser(id, HttpContext.User.Identity.Name))
                    {
                        ContentResult contentResult = new ContentResult();
                        String rss = dataManager.GetRss(id, "http://localhost/ILoveRss/Home/ViewChannel/" + id.ToString());
                        contentResult = Content(@"<?xml version=""1.0"" encoding=""utf-8""?>" + @"<rss version=""2.0"">" + rss + "</rss>", "text/xml");
                        return contentResult;
                        
                    }
                }
                return View("UnsuccessPage");
            }
            catch (Exception exc)
            {
                return View("ErrorPage");
            }
            
        }
    }
}
